//
//  LoginViewController.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/5/18.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "LoginViewController.h"
#import "NewViewController.h"
#import "ThumbnailListViewController.h"
#import "SecretAlbumsLogic.h"
#import "ThumbnailListNavigationController.h"

@interface LoginViewController () <NewViewControllerDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = SABackgroundColor;
    self.view.tintColor = SATintColor;
    self.circleBtnTitleColor = [UIColor whiteColor];
    
    // 设置标题文本
    self.titleLable.text = NSLocalizedString(@"EnterPassword", nil);
    self.titleLable.textColor = [UIColor whiteColor];
    
    // 设置action按钮
    [self.actionBtn setTitle:NSLocalizedString(@"New", nil) forState:UIControlStateNormal];
    [self.actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.actionBtn addTarget:self action:@selector(newBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

#pragma mark - 私有方法

- (void)newBtnOnclick:(UIButton *)button
{
    [self.dotsView cleanPassWord];
    NewViewController *controller = [NewViewController new];
    controller.delegate = self;
    [self presentViewController:controller animated:YES completion:nil];
}

- (void)gotoPhotoListView
{
    ThumbnailListViewController *controller = [[ThumbnailListViewController alloc] init];
    
    ThumbnailListNavigationController *nav = [[ThumbnailListNavigationController alloc] initWithRootViewController:controller];
    
    [nav setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - DotsViewDelegate

- (BOOL)verifyPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    if ([SecretAlbumsLogic loginWithID:password]) {
        return YES;
    } else {
        self.titleLable.text = NSLocalizedString(@"PasswordError", nil);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.titleLable.text = NSLocalizedString(@"EnterPassword", nil);
        });
        
        return NO;
    }
}

- (void)successPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    [self gotoPhotoListView];
}

#pragma mark - NewViewControllerDelegate

- (void)cancelCreatePassword:(NewViewController *)newViewController
{
    [self.dotsView cleanPassWord];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)successPassword:(NSString *)password newViewController:(NewViewController *)newViewController
{
    [self.dotsView setupPassWord:password];
    [self dismissViewControllerAnimated:YES completion:^{
        if ([SecretAlbumsLogic loginWithID:password]) {
            [self gotoPhotoListView];
        }
    }];
}

@end
